// JSHumanizer, version 0.0
// Copyright © 2010, Richard Cook.
// http://jshumanizer.googlecode.com/
//
// This file is part of JSHumanizer.
 //
// JSHumanizer is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the licence or
// (at your option) any later version.
//
// JSHumanizer is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with JSHumanizer. If not, see <http://www.gnu.org/licenses/>.

var JSHumanizer = (function(self) {

  self.__zeroPad = function(value, width) {
    var str = value + "";
    while (str.length < width) {
      str = "0" + str;
    }
    return str;
  };

  self.__humanizeIntHelper = function(value, grouping_length, group_separator) {
    value = parseInt(value) + "";

    var text = "";
    var segment_length = value.length % grouping_length;
    for (var i = 0; i < value.length; ) {
      if (i > 0) {
        text += group_separator;
      }
      text += value.substr(i, segment_length);
      i += segment_length;
      segment_length = grouping_length;
    }
    return text;
  };

  self.__humanizeDecimalHelper = function(value, decimal_places, decimal_separator, grouping_length, group_separator) {
    var segments = value.toFixed(decimal_places).split(".");
    return self.__humanizeIntHelper(segments[0], grouping_length, group_separator)
      + decimal_separator
      + segments[1];
  };

  self.humanizeBytes = function(value) {
    value = parseInt(value);

    if (value >= 1073741824) {
      return self.humanizeInt(value / 1073741824) + " GB";
    }
    if (value >= 1048576) {
      return self.humanizeInt(value / 1048576) + " MB";
    }
    if (value >= 1024) {
      return self.humanizeInt(value / 1024) + " kB";
    }
    return self.humanizeInt(value) + " bytes";
  };

  return self;
}(JSHumanizer || {}));

